#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import json

sys.path.append(
    os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "imlfapi")
)


import requests
from db import UsersTB

# 测试
base_url = "http://localhost:8000/imlf/api/v1/"
# 线上
# base_url = 'http://10.12.3.162:32488/imlf/api/v1/'

headers = {
    "token": "",
}


def test_login():
    url = base_url + "users/login"
    data = dict(username="张秋月", password="123456")
    res = requests.post(url, headers=headers, json=data)
    content = json.loads(res.content)
    print(content)


def test_token_expire():
    import time

    url = base_url + "users/login"
    data = dict(username="郑水清", password="123456")
    res = requests.post(url, json=data)
    content = json.loads(res.content)
    token = content["dataset"]["token"]
    print(token)
    time.sleep(7)
    tree_url = base_url + "/entites/tree"
    res = requests.get(tree_url, headers={"token": token})
    print("code_status")
    print(res.status_code)
    content = json.loads(res.content)
    print(content)
    # print(content['msg'])
    pass


def test_register():
    url = base_url + "users/register"
    # 数据准备
    data = {
        "email": "zhengshuishui@goldwind.com",
        "username": "郑水水",
        "password": "123456",
        "register_code": "hydk",
    }
    json_data = {
        "email": data["email"],
        "username": data["username"],
        "password": data["password"],
        "registerCode": data["register_code"],
    }
    try:
        # 请求和验证返回
        res = requests.post(url, headers=headers, json=json_data)
        content = json.loads(res.content)
        assert content["status"] == 1
        # 验证结论
        user_from_db = UsersTB.find(username=data["username"])
        assert len(user_from_db) == 1
        pass
    except Exception as e:
        raise e
    finally:
        # 清除多余数据
        # db.UsersTB.rm(username=data["username"])
        pass

    pass


def test_retrieve_password():
    url = base_url + "users/retrievepassword"
    # 数据准备
    data = {
        "email": "zhengshuiqing@goldwind.com",
        "username": "郑水清",
    }
    json_data = {
        "email": data["email"],
        "username": data["username"],
    }
    try:
        # 请求和验证返回
        res = requests.post(url, headers=headers, json=json_data)
        content = json.loads(res.content)
        assert content["status"] == 1
        # 验证结论
    except Exception as e:
        raise e
    finally:
        # 清除多余数据
        # db.UsersTB.rm(username=data["username"])
        pass


def test_reset_password():
    url = base_url + "users/resetpassword"
    # 数据准备
    json_data = {
        "token": "eyJhbGciOiJIUzUxMiIsImlhdCI6MTYzOTExMzQ1OSwiZXhwIjoxNjM5MTE1MjU5fQ.eyJ1c2VybmFtZSI6IumDkeawtOa4hSJ9.yPj3DB7ElJBTb0UnTeGNWRpfp90KI-IULtXNQfBlOaWx5wiwxcFe2ntMvv1SJ0C6cVAnvZNclvVkcCUnPnmeZA",
        "password": "666666",
    }
    try:
        # 请求和验证返回
        res = requests.post(url, headers=headers, json=json_data)
        content = json.loads(res.content)
        assert content["status"] == 1
        # 验证结论
    except Exception as e:
        raise e
    finally:
        # 清除多余数据
        # db.UsersTB.rm(username=data["username"])
        pass


if __name__ == "__main__":
    test_token_expire()
    pass
